import winreg as reg
import os
import ctypes

def add_to_reg(): 
    # 获取当前工作目录
    current_directory = os.getcwd()
    
    # 打开 'HKEY_CLASSES_ROOT\Directory\Background\shell' 键
    key_path = r"Directory\Background\shell"
    key = reg.OpenKey(reg.HKEY_CLASSES_ROOT, key_path, 0, reg.KEY_ALL_ACCESS)
    
    # 创建新键
    new_key = reg.CreateKey(key, "复制当前目录")
    reg.SetValueEx(new_key, '', 0, reg.REG_SZ, "获取当前目录")
    reg.SetValueEx(new_key, 'Icon', 0, reg.REG_SZ, current_directory + "\\get_current_dir.exe.exe")
    
    # 创建命令子键
    command_key = reg.CreateKey(new_key, "command")
    reg.SetValueEx(command_key, '', 0, reg.REG_SZ, '"' + current_directory + '\\get_current_dir.exe" "'+current_directory)

def need_admin():
    # 定义ShellExecute相关常量
    SEE_MASK_FLAG_DDEWAIT = 0x00000100
    SEE_MASK_FLAG_NO_UI = 0x00000400
    SEE_MASK_NOCLOSEPROCESS = 0x00000040
    SEE_MASK_UNICODE = 0x00004000
    SEE_MASK_NO_CONSOLE = 0x00008000
    SW_SHOWNORMAL = 1
    ERROR_CANCELLED = 1223

    # 执行需要管理员权限的命令或程序
    try:
        result = ctypes.windll.shell32.ShellExecuteW(None, "runas", "cmd.exe", None, None, SW_SHOWNORMAL)
        if result <= 32:
            raise ctypes.WinError()
    except OSError as e:
        if e.winerror == ERROR_CANCELLED:
            print("用户取消了操作")
        else:
            print("错误：%s" % e.strerror)
    else:
        print("已成功申请管理员权限")

def check_root():
    ## linux os.geteuid()
    
    if ctypes.windll.shell32.IsUserAnAdmin():
        print("当前用户是超级用户（root）")
         
    else:
        print("当前用户是普通用户")
        need_admin()
    
if __name__ == "__main__":
    # vars = input("确定要添加注册表吗Y/N")
    # if vars.index
    # vars= vars.casefold()
    # check_root()
    add_to_reg()
    # add_to_reg()